
namespace MGFramewor {
    const { ccclass, property } = cc._decorator;

    export enum Direction {
        horizontal = 0,
        vertical = 1,
    }

    @ccclass
    export class ScollerView extends cc.Component {

        @property({ type: cc.Enum(Direction), displayName: "滚动方向", tooltip: "滚动方向，默认为竖直方向。" })
        direction: Direction = Direction.horizontal;

        @property({ type: cc.Prefab, displayName: "展示对象", tooltip: "滚动列表展示对象。" })
        item: cc.Prefab = null;

        @property({ type: cc.Float, displayName: "对象高度", tooltip: "展示对象高度,当值小于等于0时，默认使用原始高度。" })
        height: number = 0;

        @property({ type: cc.Float, displayName: "对象宽度", tooltip: "展示对象宽度,当值小于等于0时，默认使用原始宽度。" })
        width: number = 0;

        @property({ type: cc.Float, displayName: "对象间隔", tooltip: "展示对象之间的间隔,默认为0。" })
        interval: number = 0;

        /**
         * 滚动列表宽度
         */
        private scoller_width;

        /**
         * 滚动列表高度
         */
        private scoller_height;

        /**
         * 滚动列表宽度
         */
        private item_width;

        /**
         * 滚动列表高度
         */
        private item_height;

        /**
         * 对象列表
         */
        private item_list: Array<cc.Node>;

        /**
         * 对象数据列表
         */
        private item_data_list: Array<cc.Node>;

        /**
         * 起始位置
         */
        private start_location: number;

        start() {
            console.log(this.item);
            this.InitScroller();
        }

        /**
         * 初始化滚动列表
         */
        private InitScroller() {

            this.scoller_width = this.node.width;
            this.scoller_height = this.node.height;

            this.item_width = this.width > 0 ? this.width : this.item.data.width;
            this.item_height = this.height > 0 ? this.height : this.item.data.height;

            this.item_list = new Array<cc.Node>();

            switch (this.direction) {
                case Direction.vertical:
                    this.CreateItem_vertical();
                    break;
                case Direction.horizontal:
                    this.CreateItem_horizontal();
                    break;
            }
        }

        /**
         * 创建对象（竖直方向滚动）
         */
        private CreateItem_vertical() {
            this.start_location = this.scoller_height * 0.5 - this.item_height * 0.5;
            this.CreateItem(0, this.start_location - this.interval);
        }

        /**
         * 创建对象（水平方向滚动）
         */
        private CreateItem_horizontal() {

        }

        /**
         * 创建对象
         * @param x x轴坐标
         * @param y y轴坐标
         */
        private CreateItem(x:number, y:number) {
            let node = cc.instantiate(this.item);
            node.setParent(this.node);
            node.position = new cc.Vec3(x, y);
            console.log(node.position);
        }
    }
}
